package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;

public class PopWindowActivity extends AppCompatActivity implements View.OnClickListener {

    private View tv_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popwindow);
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        tv_1 = findViewById(R.id.tv_1);
        tv_1
                .setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        View view = getLayoutInflater().inflate(R.layout.layout_popwindow, null);
        PopupWindow popupWindow = new PopupWindow(view, tv_1.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
        view.findViewById(R.id.tv_1).setOnClickListener(v1 -> {
            popupWindow.dismiss();
            Toast.makeText(this,"点击了第一项",Toast.LENGTH_LONG).show();
        });
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.showAsDropDown(tv_1);
    }
}